package com.bjc.core.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bjc.core.bean.request.UserLoginReqVO;
import com.bjc.core.bean.response.UserInfoVO;
import com.bjc.core.bean.response.UserLoginVO;
import com.bjc.core.bean.response.WxCpUser;
import com.bjc.core.dao.IUserLoginDao;
import com.bjc.core.service.IUserLoginService;
import com.bjc.core.tool.service.IRedisService;

import lombok.extern.slf4j.Slf4j;



@Service
@Slf4j
public class UserLoginServiceImpl implements IUserLoginService {
	
	
	private static String imgbase = "";
	
	@Autowired
	private IUserLoginDao userMapper;
	
	@Autowired
	protected IRedisService redisService;
	
	@Override
	public UserLoginVO getUserByWorkId(String workId) {
		UserLoginVO user = userMapper.getUserByWorkId(workId);
//		try {
//			JSONObject jsonuser = HttpUtil.sendPost(ToolUtil.getAppConfig("domain") + "/wechat/user/getUser", "POST", workId);
//			user.setAvatar(jsonuser != null && jsonuser.get("avatar")!=null?jsonuser.get("avatar").toString():imgbase);
//		} catch (Exception e) {
//			log.error("获取头像出错");
//		}
		 log.info("根据关键字获取User对象");
		return user;
	}
	
	@Override
	public Map<String, Object> selectLevel(Map<String, Object> map) {
		return userMapper.selectLevel(map);
	}
	
	@Override
	public UserLoginVO selectByPrimaryKey(String workId) {
		return userMapper.selectByPrimaryKey(workId);
	}
	
	@Override
	public Double selectTotalHours(String workId) {
		return userMapper.selectTotalHours(workId);
	}
	@Override
	public List<String> selectByParentWorkId(Map<String, Object> map) {
		return userMapper.selectByParentWorkId(map);
	}
	
	@Override
	public String getUserByLoginName(String username) {
		return userMapper.getUserByLoginName(username);
	}
	
	@Override
	public UserLoginVO getUsers(String workId) {
		return userMapper.getUsers(workId);
	}
	
	@Override
	public UserLoginVO getLoginUse(String username) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("username", username);
		UserLoginVO user = userMapper.getLoginUse(map);
		if(user !=null){
			user.setAvatar(imgbase);
		}else{
			user = new UserLoginVO();
			user.setUserName(username);
			user.setWorkId(username);
			user.setAvatar(imgbase);
		}
		
		return user;
	}

	@Override
	public List<UserLoginVO> getUserAll() {
		return userMapper.getUserAll();
	}
	@Override
	public UserLoginVO getUser(UserLoginReqVO vo) {
		return userMapper.getUser(vo);
	}

	@Override
	public Integer chenkBlack(Integer userId) {
		return userMapper.chenkBlack(userId);
	}

	@Override
	public Integer chenkDismiss(Integer userId) {
		return userMapper.chenkBlack(userId);
	}

	@Override
	public Integer chenkUserOut(Integer userId) {
		return userMapper.chenkUserOut(userId);
	}

	@Override
	public List<UserLoginVO> findChildUser(String workId,String workName) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("adminId", workId);
		if(StringUtils.isNoneBlank(workName)){
			map.put("workName", workName);
		}
		map.put("userWorkId", workId);
		List<UserLoginVO> childList = userMapper.findChildUser(map);
		if(childList != null && childList.size()>0){
			//查询微信头像
			for (int i = 0; i < childList.size(); i++) {
				if(childList.get(i) != null && StringUtils.isNotBlank(childList.get(i).getWxuId())){
					WxCpUser wxUser = redisService.getWxuser(childList.get(i).getWxuId());
					if(wxUser != null && StringUtils.isNotBlank(wxUser.getAvatar())){
						childList.get(i).setAvatar(wxUser.getAvatar());
					}else{
						childList.get(i).setAvatar(imgbase);
					}
				}
			}
		}
		return childList;
	}

	@Override
	public UserInfoVO selectByWorkId(String workId) {
		return userMapper.selectByWorkId(workId);
	}
}
